package com.example.exam.controller;

import com.example.exam.entity.*;
import com.example.exam.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * (Torf)表控制层
 *
 * @author makejava
 * @since 2021-09-04 15:02:45
 */
@RestController
@RequestMapping("/torf")
public class TorfController {
    /**
     * 服务对象
     */
    @Autowired
    private TorfService torfService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private PaperService paperService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne/{id}")
    public Torf selectOne(@PathVariable("id") Integer id) {
        return this.torfService.queryById(id);
    }

    @RequestMapping("/selectAll")
    public List<Torf> selectAll() {
        return this.torfService.queryAll();
    }

    /**
     * 通过传入记录开始数，对其进行一页10个展示。
     *
     * @param page 记录开始数
     * @return 那一页的10条数据
     */
    @RequestMapping("/selectByPaper")
    public List<Torf> selectByPaper(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, String question, String subject) {
        return this.torfService.queryAllByLimit(page, limit, question, subject);
    }

    @RequestMapping("/deleteOne/{id}")
    public boolean deleteOne(@PathVariable("id") Integer id) {
        return this.torfService.deleteById(id);
    }

    @RequestMapping("/insertOne")
    public Torf insertOne(Torf Torf) {
        return torfService.insert(Torf);
    }

    @RequestMapping("/update")
    public Torf update(Integer id, Torf torfVal,
                       HttpSession session) {
        Torf Torf = torfService.queryById(id);
        Torf.setAnswer(torfVal.getAnswer());
        Torf.setCheck(torfVal.getCheck());
        Torf.setQuestion(torfVal.getQuestion());
        /*
        查找老师对应专业
         */
        int userId = (int) session.getAttribute("userId");
        boolean flag = false;
        String subject = torfVal.getSubject();
        if (userId < 4000 && userId > 3000) {
            Teacher teacher = teacherService.queryById(userId);
            if (teacher != null) {
                String teaSubject = teacher.getInstitute();
                College college = collegeService.queryByName(teaSubject);
                int collegeId = college.getcollegeId();
                List<Major> lists = majorService.queryByCollegerId(collegeId);
                Set<String> set = new HashSet<>();
                for (Major list : lists) {
                    set.add(list.getMajorname());
                }
                if (set.contains(subject)) {
                    flag = true;
                }
            } else {
                return null;
            }
        } else {
            return null;
        }
        if (flag == true) {
            Torf.setSubject(torfVal.getSubject());
        } else {
            return null;
        }
        return this.torfService.update(Torf);
    }

    /**
     * 根据传入专业查询
     *
     * @param subject
     * @return
     */
    @RequestMapping("/selectSubject")
    public List<Torf> selectSub(String subject) {
        List<Torf> list = torfService.queryBySubject(subject);
        return list;
    }

    /**
     * 模糊查询
     *
     * @param question
     * @return
     */
    @RequestMapping("/likeTorf")
    public List<Torf> selectlike(String question) {
        List<Torf> list = torfService.queryByChLike(question);
        return list;
    }

    /**
     * 老师端显示填空题
     *
     * @param session
     * @return
     */
    @RequestMapping("/showTo")
    public List<Torf> showTeaTo(HttpSession session) {
        Integer questionId = (Integer) session.getAttribute("questionId");
        String paperquestion = paperService.queryById(questionId).getPaperquestion();
        String[] split = paperquestion.split(",");
        List<Torf> list = new LinkedList<>();
        for (String s : split) {
            int temp = Integer.parseInt(s);
            if (temp < 3000 && temp > 2000) {
                list.add(torfService.queryById(temp));
            }
        }
        return list;
    }

    /**
     * 老师根据点击试题展示判断
     *
     * @param questionId
     * @return
     */
    @RequestMapping("/showTeaTorf")
    public List<Torf> showTeaTorf(Integer questionId) {
        Integer paperid = questionService.queryById(questionId).getPaperid();
        Paper paper = paperService.queryById(paperid);
        String question = paper.getPaperquestion();
        String[] split = question.split(",");
        List<Torf> res = new LinkedList<>();
        for (String s : split) {
            int temp = Integer.parseInt(s);
            if (temp > 2000 && temp < 3000) {
                res.add(torfService.queryById(temp));
            }
        }
        return res;
    }

    @RequestMapping("/showTorf")
    public List<Torf> showTorf(Integer stuId, Integer questionId) {
        Integer paperid = 101;//questionService.queryById(questionId).getPaperid();
        Paper paper = paperService.queryById(paperid);
        String question = paper.getPaperquestion();
        String[] split = question.split(",");
        List<Torf> res = new LinkedList<>();
        for (String s : split) {
            int temp = Integer.parseInt(s);
            if (temp > 2000 && temp < 3000) {
                res.add(torfService.queryById(temp));
            }
        }
        return res;
    }

    @RequestMapping("/correctTorf")
    public int correctCh(int[] num, HttpSession session) {
        int ans = 0;
        Integer res = (Integer) session.getAttribute("res");
        if (res == null) {
            for (int i : num) {
                if (i == 1) {
                    ans += 5;
                }
            }
            session.setAttribute("session", ans);
        } else {
            for (int i : num) {
                res += i;
            }
            ans = res;
            session.setAttribute("res", res);
        }
        if (ans == 0) {
            return 0;
        } else {
            return 1;
        }

    }
}